﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace POSPlus.DatabaseOperations
{
	/// <summary>
	/// Executes a not query type of command on the SQL database. An example of this is table creation,
	/// inserting rows or updating rows.
	/// </summary>
	/// <remarks>
	/// This class has been depreciated: Extend the DatabaseObject class based on the information
	/// needed from the database.
	/// </remarks>
	public class DatabaseNonQueryExecutor : DatabaseOperationExecutor
	{
		#region Properties
		public override int RecordCount
		{
			get { return _affectedRows; }
		}
		#endregion

		#region Members
		private int _affectedRows;
		#endregion

		#region Constructors
		public DatabaseNonQueryExecutor(String server, String user, String password, String database) :
			base(server, user, password, database)
		{
			_affectedRows = -1;
		}
		#endregion

		#region Methods
		public override void Execute()
		{
			try
			{
				_connectionManager.Open();
			}
			catch (InvalidOperationException)
			{
				throw new InvalidOperationException("Unable to open connection. Connection State: " +
					_connectionManager.ToString());
			}
			catch (SqlException)
			{
				throw;
			}

			using (SqlCommand command = new SqlCommand(CommandString, _connectionManager.Connection))
			{
				try
				{
					_affectedRows = command.ExecuteNonQuery();
				}
				catch (SqlException)
				{
					throw;
				}
			}

			try
			{
				_connectionManager.Close();
			}
			catch (SqlException)
			{
				throw;
			}
		}
		#endregion
	}
}
